﻿using Microsoft.Extensions.DependencyInjection;
using Puppy.DependencyInjection;
using System;
using System.Collections.Concurrent;

namespace Puppy.ActorDb
{
    public class ActorDatabaseManager : ISingletonDependency
    {
        private readonly ConcurrentDictionary<string, IActorDatabase> _databases =
            new ConcurrentDictionary<string, IActorDatabase>();

        private readonly IServiceProvider _serviceProvider;

        public ActorDatabaseManager(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public IActorDatabase Get(string databaseName)
        {
            return _databases.GetOrAdd(databaseName, _ => _serviceProvider.GetRequiredService<IActorDatabase>());
        }
    }
}
